package org.jeecg.modules.ywEndFinishedProductInventory.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.constant.CommonConstant;
import org.jeecg.constant.InventoryConstant;
import org.jeecg.modules.ywEndFinishedProductInventory.entity.YwEndFinishedProductInventoryInfo;
import org.jeecg.modules.ywEndFinishedProductInventory.mapper.YwEndFinishedProductInventoryInfoMapper;
import org.jeecg.modules.ywEndFinishedProductInventory.service.IYwEndFinishedProductInventoryInfoService;
import org.jeecg.modules.ywEndFinishedProductInventory.vo.MobileInventoryRequest;
import org.jeecg.modules.ywEndFinishedProductOut.entity.YwEndFinishedProductOutDetail;
import org.jeecg.modules.ywEndFinishedProductOut.entity.YwEndFinishedProductOutMain;
import org.jeecg.modules.ywEndFinishedProductOut.service.IYwEndFinishedProductOutDetailService;
import org.jeecg.modules.ywEndFinishedProductOut.service.IYwEndFinishedProductOutMainService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 完成品库存
 * @Author: jeecg-boot
 * @Date: 2024-08-26
 * @Version: V1.0
 */
@Service
public class YwEndFinishedProductInventoryInfoServiceImpl extends ServiceImpl<YwEndFinishedProductInventoryInfoMapper, YwEndFinishedProductInventoryInfo> implements IYwEndFinishedProductInventoryInfoService {

    @Resource
    private YwEndFinishedProductInventoryInfoMapper ywEndFinishedProductInventoryInfoMapper;
    @Resource
    private IYwEndFinishedProductOutMainService ywEndFinishedProductOutMainService;
    @Resource
    private IYwEndFinishedProductOutDetailService ywEndFinishedProductOutDetailService;

    @Override
    public void mobile(MobileInventoryRequest mobileInventoryRequest) {
        // 想要移库的那条库存
        YwEndFinishedProductInventoryInfo inventoryInfo = this.ywEndFinishedProductInventoryInfoMapper.selectById(mobileInventoryRequest.getInventoryId());
        Assert.notNull(inventoryInfo, "不存在的库存信息,请联系管理员！");
        Assert.isTrue(inventoryInfo.getStatus().equals(CommonConstant.StatusEnum.YES.getCode()), "库存信息已被删除,请刷新后重试！");
        Assert.isTrue(mobileInventoryRequest.getInventoryNum() <= inventoryInfo.getInventoryNum(), "移库数量不能大于库存数量！");

        // 新增移库的库存
        YwEndFinishedProductInventoryInfo inventory = new YwEndFinishedProductInventoryInfo();
        inventory.setHistoryId(inventoryInfo.getId());
        inventory.setEnterId(inventoryInfo.getEnterId());
        inventory.setEnterDate(inventoryInfo.getEnterDate());
        inventory.setModelCode(inventoryInfo.getModelCode());
        inventory.setBoxVolume(inventoryInfo.getBoxVolume());
        inventory.setDirectionCode(inventoryInfo.getDirectionCode());
        inventory.setManufactureDate(inventoryInfo.getManufactureDate());
        inventory.setStatus(CommonConstant.StatusEnum.YES.getCode());
        inventory.setInventoryNum(mobileInventoryRequest.getInventoryNum());
        inventory.setInventoryVolume(inventory.getBoxVolume().multiply(BigDecimal.valueOf(inventory.getInventoryNum())));
        inventory.setGoodsPosition(mobileInventoryRequest.getGoodsPosition());
        this.ywEndFinishedProductInventoryInfoMapper.insert(inventory);

        // 更新现有库存
        inventoryInfo.setInventoryNum(inventoryInfo.getInventoryNum() - mobileInventoryRequest.getInventoryNum());
        inventoryInfo.setInventoryVolume(inventoryInfo.getInventoryVolume().subtract(inventory.getInventoryVolume()));
        this.ywEndFinishedProductInventoryInfoMapper.updateById(inventoryInfo);
    }

    @Override
    public List<YwEndFinishedProductInventoryInfo> selectList(String modelCode, String directionCode) {
        return this.ywEndFinishedProductInventoryInfoMapper.getList(modelCode, directionCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outBatch(List<String> wantOutIdList, Date outDate, String billNo) {
        List<YwEndFinishedProductInventoryInfo> inventoryInfoList = this.ywEndFinishedProductInventoryInfoMapper.selectBatchIds(wantOutIdList);
        Assert.isTrue(inventoryInfoList.size() == wantOutIdList.size(), "出库失败,请检查数据是否异常！");

        YwEndFinishedProductOutMain outMain = new YwEndFinishedProductOutMain();
        outMain.setOutDate(outDate);
        outMain.setGoodsFlowDirection("直接出库");
        outMain.setOutStatus(InventoryConstant.OutStatusEnum.PREPARE_OUT.getCode());
        ywEndFinishedProductOutMainService.save(outMain);

        List<YwEndFinishedProductOutDetail> outDetailList = inventoryInfoList.stream().map(data -> {
            YwEndFinishedProductOutDetail outDetail = new YwEndFinishedProductOutDetail();
            outDetail.setOutId(outMain.getId());
            outDetail.setBillNo(billNo);
            outDetail.setModelCode(data.getModelCode());
            outDetail.setBoxVolume(data.getBoxVolume());
            outDetail.setDirectionCode(data.getDirectionCode());
            outDetail.setOutNum(data.getInventoryNum());
            outDetail.setInventoryId(data.getId());
            outDetail.setEnterDate(data.getEnterDate());
            outDetail.setGoodsPosition(data.getGoodsPosition());
            outDetail.setInventoryNum(data.getInventoryNum());
            outDetail.setOutVolume(data.getInventoryVolume());
            outDetail.setStatus(CommonConstant.StatusEnum.YES.getCode());

            data.setInventoryNum(0);
            return outDetail;
        }).collect(Collectors.toList());
        ywEndFinishedProductOutDetailService.saveBatch(outDetailList);

        this.updateBatchById(inventoryInfoList);
    }

}
